<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $userid
 * @property string $username
 * @property string $name
 * @property integer $age
 * @property integer $gender
 * @property string $certno
 * @property string $headpic
 * @property string $infopic
 * @property string $desc
 * @property string $password
 * @property string $salt
 * @property integer $create_timestamp
 * @property integer $modify_timestamp
 * @property integer $modify_userid
 */
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
    public $authKey;
    public $accessToken;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['age', 'gender', 'create_timestamp', 'modify_timestamp', 'modify_userid'], 'integer'],
            [['name'], 'string', 'max' => 32],
            [['username', 'password'], 'required'],
            [['username'], 'unique'],
            [['desc'], 'string'],
            [['headpic', 'infopic'], 'string', 'max' => 255],
            [['username'], 'string', 'max' => 32],
            [['certno'], 'string', 'max' => 20],
            [['password'], 'string', 'max' => 40],
            [['salt'], 'string', 'max' => 8]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'userid' => Yii::t('app', 'Userid'),
            'username' => Yii::t('app', 'Username'),
            'name' => Yii::t('app', 'Name'),
            'headpic' => Yii::t('app', 'Head Pic'),
            'infopic' => Yii::t('app', 'Info Pic'),
            'age' => Yii::t('app', 'Age'),
            'gender' => Yii::t('app', 'Gender'),
            'certno' => Yii::t('app', 'Certno'),
            'desc' => Yii::t('app', 'desc'),
            'password' => Yii::t('app', 'Password'),
            'salt' => Yii::t('app', 'Salt'),
            'create_timestamp' => Yii::t('app', 'Create Timestamp'),
            'modify_timestamp' => Yii::t('app', 'Modify Timestamp'),
            'modify_userid' => Yii::t('app', 'Modify Userid'),
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::findOne(['access_token' => $token]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->userid;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    public function save($runValidation = true, $attributeNames = null){
        if($this->isNewRecord){
            $salt = $this->saltGenerator();
            $this->setAttribute('salt', $salt);
            $this->setAttribute('password', sha1($this->password.$salt));
            $this->setAttribute('create_timestamp', time());
        }

        $this->setAttribute('modify_timestamp', time());
        $this->setAttribute('modify_userid', \Yii::$app->user->id);//$app->user->identity is usermodel
        return parent::save($runValidation, $attributeNames);
    }

    private function saltGenerator(){
        $salt = "";
        for ($i = 0; $i < 8; $i++)
        {
            $salt .= chr(mt_rand(33, 126));
        }
        return $salt;
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return $this->password === sha1($password.$this->salt);
    }

    public static function findByUsername($username){
        return static::findOne(['username' => $username]);
    }

    public function getModifyUsername(){
        $modifyUser = $this->hasOne(self::className(), ['userid' => 'modify_userid'])->one();
        return $modifyUser->username;
    }
}
